<template>
	<uni-nav-bar
		left-icon="left"
		:title="`我的打样记录(${list.length || 0})`"
		fixed
		@clickLeft="goBack"
		height="100rpx"
	/>
	<view class="print-record-container">
		<view class="show-list">
			<f-scroll-list
				:status="loadMore.status"
				:showIcon="loadMore.showIcon"
				:color="loadMore.color"
				:contentText="loadMore.contentText"
				:useLoadMore="list.length > 0 ? true : false"
			>
				<f-print-singleGoods-card :goodsList="list" @getCurrentItem="getCurrentItem" />
			</f-scroll-list>
		</view>
	</view>
</template>

<script setup>
	import { ref, toRaw, reactive } from 'vue';
	import sheep from '@/sheep';
	import { onReachBottom, onPullDownRefresh, onLoad } from '@dcloudio/uni-app';
	import _ from 'lodash';
	import printRecordApi from './../../sheep/api/printRecord.js';
	const loadMore = reactive({
		status: '',
		showIcon: true,
		color: '',
		contentText: {},
	});
	// mock数据
	const mockData = [];
	const list = ref([]);
	const pageIndex = ref(1);
	const total = ref(0);
	const goBack = () => {
		sheep.$router.back();
	};
	const getCurrentItem = (item) => {
		sheep.$router.go('/pages/index/goodsDetail', item);
	};
	const getDataList = (val = '0') => {
		let data = {
			pageNumber: pageIndex.value,
			pageSize: 10,
		};
		printRecordApi.getPrintRecordList(data).then((res) => {
			if (val != '1') {
				//下拉时，查询到的数据，不需要合并到列表
				if (res.data.records.length) {
					list.value = _.concat(list.value, res.data.records);
					total.value = res.data.total;
				} else {
					return uni.showToast({
						title: '暂未查询到相关数据',
						icon: 'none',
					});
				}
			} else {
				list.value = res.data.records;
			}
		});
	};
	/**
	 * reachBottom 触底事件
	 */
	onReachBottom(() => {
		loadMore.status = 'loading';
		pageIndex.value += 1;
		if (list.value.length !== total.value) {
			loadMore.status = 'more';
			getDataList();
		} else {
			loadMore.status = 'no-more';
		}
	});
	const loading = ref(null);
	onPullDownRefresh(async () => {
		loading.value = true;
		uni.showLoading({
			title: '正在刷新...',
			mask: true,
			fail: () => {
				uni.hideLoading();
			},
		});
		await sheep.$store('app').init();
		setTimeout(function () {
			uni.stopPullDownRefresh();
			loading.value = false;
			uni.hideLoading();
		}, 800);
		pageIndex.value = 1;
		getDataList('1');
	});
	onLoad(() => {
		getDataList();
	});
</script>

<style scoped lang="scss">
	.print-record-container {
		padding: 16rpx 32rpx;

		.show-list {
			margin-top: -8rpx;
		}
	}
</style>
